package com.qf.servlet;

import com.qf.convert.MyConvert;
import com.qf.pojo.Admin;
import com.qf.pojo.PageBean;
import com.qf.pojo.User;
import com.qf.service.AdminService;
import com.qf.service.impl.AdminServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet("/admin")
public class AdminServlet extends baseServlet {
    public void login(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //1)获取前台用户提交的参数
        String name = request.getParameter("name");
        String password = request.getParameter("password");

        // System.out.println(name+"---"+password) ;

        //封装管理实体
        Admin admin = new Admin() ;
        admin.setName(name) ;
        admin.setPassword(password) ;

        //调用业务层接口
        AdminService adminService = new AdminServiceImpl() ;
        Admin loginAdmin = adminService.isLogin(admin) ;

        if(loginAdmin!=null){
            //true 成功

            //用户登录成功之后,需要将用户的信息 存储session中(存储服务器)
            //通过请求对象获取HttpSession, 将用户存储到服务器端
            // HttpSession getSession();
            HttpSession session = request.getSession();

            session.setAttribute("admin",loginAdmin);


            //请求转发 到admin_index.jsp
            request.getRequestDispatcher("/admin/admin_index/admin_index.jsp").forward(request,response);

        }else{
            //请求转发login.jsp
            request.setAttribute("msg","用户名或者密码输入错误") ;
            request.getRequestDispatcher("/admin/login.jsp").forward(request,response);
        }
    }
    public void findUser(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //调用AdminService
        AdminService adminService = new AdminServiceImpl() ;
        List<User> list  =adminService.getAllUser() ;

        //直接将list存储request域对象中
        request.setAttribute("list",list) ;
        //请求转发到
        request.getRequestDispatcher("/admin/admin_index/user_list.jsp").forward(request,response); ;

    }
   public void findUserByPage(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
       //1)接收参数

       String curPage = request.getParameter("currentPage"); //接收当前页码
       String pSize = request.getParameter("pageSize");//接收每页显示的条数

       //判断
       if(curPage==null || "".equals(curPage)){
           //给默认值
           curPage = "1" ;
       }
       if(pSize== null || "".equals(pSize)){
           //给默认值
           pSize = "2" ;
       }

       //将curPage和pSize转换成 int类型
       int currentPage = Integer.parseInt(curPage) ;
       int pageSize = Integer.parseInt(pSize);

       //2)调用业务接口,获取分页实体 PageBean
       AdminService adminService = new AdminServiceImpl() ;
       PageBean<User> pb = adminService.findUserByPage(currentPage, pageSize);
       System.out.println(pb);
       //3)将PageBean对象存储在request域中,请求转发到admin/amdin_index/user_list.jsp
       request.setAttribute("pb",pb) ;
       request.getRequestDispatcher("/admin/admin_index/user_list.jsp").forward(request,response);
   }
   public void updateUser(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
       //获取当前表单提交的所有参数的Map集合
       Map<String, String[]> parameterMap = request.getParameterMap();
       //创建User对象
       User user = new User() ;

       //使用common-beanutils.jar ----.有一个转换器工具类--->开发者可以自定义日期转换器
       ConvertUtils.register(new MyConvert(), Date.class); //注册转换器
       //参数1:Convert接口---> 自定义一个类实现接口,重写里面convert方法
       //参数2:转换成最终类型 的Class  (Date类型)

       //使用工具类
       try {
           //封装User对象了
           BeanUtils.populate(user,parameterMap) ;
       } catch (IllegalAccessException e) {
           e.printStackTrace();
       } catch (InvocationTargetException e) {
           e.printStackTrace();
       }
       // System.out.println(user);
       //调用业务接口
       AdminService adminService = new AdminServiceImpl() ;
       adminService.updateUser(user) ;

       //请求转发到查询所有的用户的后台
       request.getRequestDispatcher("/admin?methodName=findUser").forward(request,response);
   }
   public void delUser(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
       //1)接收地址携带的参数
       String uid = request.getParameter("uid");
       //System.out.println(uid);

       //2)调用AdminService业务接口
       AdminService adminService = new AdminServiceImpl() ;
       adminService.delUser(uid) ;

       //请求转发到  "/findAllUser"
       request.getRequestDispatcher("/admin?methodName=findUser").forward(request,response);
   }
}
